unit cpu;

interface
uses graph, crt, constant,draw,utils;
var 
  CopyAKK, CopyRgD: longint;
procedure Interpretator;
procedure NewValue;
implementation
procedure Add;
begin
 case NumberOfTact of
   8: RgA:=Aisp;
   9: RgD:=Memory[RgA];
   10: begin
         CopyAKK:=AKK;
         CopyRgD:=RgD;
         AKK:=AKK+RgD;
       end;
   11: begin
        if AKK=0 
        then 
          ZF:=1
         else 
          ZF:=0;
        Loan:=CopyAKK+CopyRgD;
        Loan:=Loan and not $FFFF;
        if Loan<>0
        then 
          CF:=1
         else 
          CF:=0;
       end;
 end;
end;

procedure Sub;
var akkl,rgdl:longint;
begin
  case NumberOfTact of
   8: RgA:=Aisp;
   9: RgD:=Memory[RgA];
   10: begin
        CopyAKK:=AKK;
        CopyRgD:=RgD;
        akkl:=akk;rgdl:=rgd;
        AKK:=$FFFF and(akkl-rgdl)
       end;
   11: begin
        if AKK=0 
        then 
          ZF:=1 
        else
          ZF:=0;
        Loan:=CopyAKK-CopyRgD;
        Loan:=Loan and not $FFFF;
        if Loan<>0 
        then 
          CF:=1
        else 
          CF:=0;
       end;
  end;
end;

procedure Loa;
begin
 case NumberOfTact of
   8: RgA:=Aisp;
   9: RgD:=Memory[RgA];
   10: AKK:=RgD;
   11: if AKK=0
       then 
        ZF:=1
       else 
        ZF:=0;
 end;
end;

procedure Sto;
begin
 case NumberOfTact of
   8: RgA:=Aisp;
   9: RgD:=AKK;
   10: Memory[RgA]:=RgD;
 end;
end;

procedure Jz;
begin
 if ZF=1 then SchK:=Aisp;
end;

procedure Jmp;
begin
 case NumberOfTact of
   8: RgA:=Aisp;
   9: RgD:=Memory[RgA];
   10: SchK:=RgD;
 end;
end;

procedure _End;
begin
 case NumberOfTact of
   8: RgA:=Aisp;
   9: RgD:=Memory[RgA];
   10: SchK:=RgD;
   11: State:= State_Default;
 end;
end;

procedure Operation;
begin
 case KOP of
   $00: Add;
   $01: Sub;
   $02: Loa;
   $03: Sto;
   $04: Jz;
   $05: Jmp;
   $06: _End;
 end;
end;

procedure Tact;
begin
 SetColor(Black);
 OutRegistersValue;
 Inc(NumberOfTact);
 case NumberOfTact of
   1: RgA:=SchK;
   2: RgD:=Memory[RgA];
   3: RgK:=RgD;
   4: Inc(SchK);
   5: KOP:=RgK shr 8;
   6: ADR:=RgK and $00FF;
   7: Aisp:=SchK and $FF00 or ADR;
   8..11: Operation;
 end;
 if NumberOfTact=11 
 then 
  NumberOfTact:=0;
 SetColor(LightRed);
 OutRegistersValue;
end;

procedure NewValue;
var i: integer;
begin
 SetColor(Black);
 OutRegistersValue;
 State:= State_Default;
 OldState := State_Demo;
 RgA:=0;
 RgD:=0;
 NumberOfTact:=0;
 AKK:=0;
 SchK:=12;
 RgK:=0;
 ADR:=0;
 KOP:=0;
 Aisp:=0;
 ZF:=0;
 CF:=0;
 Loan:=0;
 CopyAKK:=0;
 for i:=5 to 9 do
  Memory[i]:=0;
 SetColor(LightRed);
 OutRegistersValue;
end;

procedure Interpretator;
begin
 if State<>OldState then
  OldState:=State;
 case State of
  State_Demo: begin
        UnSelectItem(selectedItemIdx);
        selectedItemIdx:=13;
        SelectItem(selectedItemIdx);
        Delay(9999 div speedValue);
        Tact;
      end;
   State_Step: begin
      Tact;
      State := State_Default;
      end;
  State_Automat: begin
       UnselectItem(selectedItemIdx);
       selectedItemIdx:=13;
       SelectItem(selectedItemIdx);
       Tact;
      end;
 end;
end;



begin
end.